<?php
// header('Content-type:text/xml;charset=gbk');

$lastTime = time();
function linkArgs($matrix){
	$data = "";
	while(list($name,$value)=each($matrix)){
		if($name == "PHPSESSID" || $name == "aazone"){
			continue ;
		}
		$data .= urlencode( $name ) . "=" . urlencode( $value ) . '&';
	}
	return $data ;
}
function showTime(){
	global $lastTime ;
	$now = time() ;
	$timelong = $now - $lastTime ;
	$lastTime = $now;
	echo "<BR><BR>--- ".$timelong;
}

$timeout = 10;		// Max time for stablish the conection
$size    = 0;		// Bytes will be read (and display). 0 for read all

$server  = '127.0.0.1';            // IP address
$host    = 'localhost';            // Domain name
$target  = '/component/aaTest.php';        // Specific program

$referer = 'http://localhost:811/';    // Referer
$port    = 811;

$getValues = "";
if(isset($_GET)){
	$getValues = "?".linkArgs($_GET);
}

$postValues = "";
if(isset($_POST)){
	$postValues = linkArgs($_POST);
}

$content = "";
$method = "POST";

showTime();

$request  = "$method $target$getValues HTTP/1.1\r\n";
$request .= "Host: $host\r\n";
$request .= 'User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) ';
$request .= "Gecko/20021204\r\n";
$request .= 'Accept: text/xml,application/xml,application/xhtml+xml,';
$request .= 'text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,';
$request .= "image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1\r\n";
$request .= "Accept-Language: en-us, en;q=0.50\r\n";
$request .= "Accept-Encoding: gzip, deflate, compress;q=0.9\r\n";
$request .= "Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66\r\n";
$request .= "Keep-Alive: 300\r\n";
$request .= "Connection: keep-alive\r\n";
$request .= "Referer: $referer\r\n";
$request .= "Cache-Control: max-age=0\r\n";

showTime();

if ($method == "POST") {
   $lenght = strlen( $postValues );
   $request .= "Content-Type: application/x-www-form-urlencoded\r\n";
   $request .= "Content-Length: $lenght\r\n";
   $request .= "\r\n";
   $request .= $postValues;
}

$aazone = "aa00";
$beginHTML = "<aa:zone id='".$aazone."'>";
$endHTML = "</aa:zone>";
$ret = '';

$len1 = strlen($beginHTML);
$len2 = strlen($endHTML);

showTime();

$socket  = fsockopen( $server, $port, $errno, $errstr, $timeout );
fputs( $socket, $request );
if ( $size > 0 ) {
	$ret = fgets( $socket, $size );
} else {
	$begin = false ;
	while ( !feof( $socket ) ) {
		$line = fgets( $socket, 64 );
		if($begin){
			$ret .= $line ;
		}else{
			$line = trim($line);
			if($beginHTML == substr($line,0,$len1)){
				$begin = true ;
			}else if($endHTML == substr($line,0,$len2)){
				break ;
			}
		}
	}
}
fclose( $socket );
showTime();


print('<?xml version="1.0" encoding="gbk"?>');
print("<aazone><![CDATA[");
print($ret);
print("]]></aazone>");

showTime();


// phpinfo();

?>